document.addEventListener('DOMContentLoaded', function() {

    var tabContents = document.querySelectorAll('.sidebar-content');

    // 处理初始hash值
    handleHashChange();

    window.addEventListener('hashchange', handleHashChange);

    // 处理hash变化的函数
    function handleHashChange() {
        // 获取当前hash值，去掉#符号
        var hash = window.location.hash.substring(1) || 'robot';

        // 隐藏所有内容
        tabContents.forEach(function (content)  {
            content.classList.add('hidden');
            content.classList.remove('fade-in');
        });

        // 显示当前hash对应的内容
        const currentContent = document.getElementById(hash);
        if (currentContent) {
            currentContent.classList.remove('hidden');
            // 使用setTimeout确保动画效果生效
            setTimeout(function()  {
                currentContent.classList.add('fade-in');
            }, 10);

            // 平滑滚动到内容顶部
            // currentContent.scrollIntoView({ behavior: 'smooth' });
        }
    }

})



//
var vue_dashboard_app = new Vue({
    el: '#robot',
    data: {
        search_str: [],
        agentList: agentList,
        form: {
            visible: false,
            agentId: '',
            agentName: '',
        },
    },
    created: function () {
        //
    },
    methods: {
        filterList: function () {
            if (this.search_str) {
                this.agentList = agentList.filter(el => el.agentName.includes(this.search_str))
            } else {
                this.agentList = agentList
            }
        },
        handleAddOne: function () {
            var f = this.form || {}
            for (var key in f) {
                if (f.hasOwnProperty(key)) {
                    f[key] = ''
                }
            }
        },
        handleCommand: function (command) {
            switch (command) {
                case 'edit':
                    //
                    break;
            }
        },
        handleEditOne: function (agent) {
            Object.assign(this.form, {
                visible: true,
                agentId: agent.id,
                agentName: agent.agentName
            })
        },
        handleSaveRobot: function () {
            var form = this.form || {}
            if (!form.agentName) return this.$message.warning('请输入机器人名称');
            var f = form.agentId > 0 ? {
                agentId: form.agentId,
                agentName: form.agentName
            } : {agentName: form.agentName || ''}
            // console.log('f->', f)
            var apiName = form.agentId > 0 ? api_robot_updateName : api_robot_create
            apiName(f, function (res) {
                if (res.code == 200) {
                    this.$message.success('操作成功');
                    this.form.visible = false
                    setTimeout(function () {
                        window.location.reload();
                    }, 1000)
                } else {
                    this.$message.error('操作失败: ' + res.message);
                }
            }.bind(this))
        },
        handleLockOne: function (agent) {
            var f = new FormData();
            f.append('agentId', agent.id);
            var apiName = agent.locked ? api_robot_unlock : api_robot_lock
            apiName(f, function (res) {
                if (res.code == 200) {
                    this.$message.success('操作成功');
                    window.location.reload();
                } else {
                    this.$message.error('操作失败: ' + res.message);
                }
            }.bind(this))
        },
        handleDelOne: function (agent) {
            if (confirm('确定要删除这个机器人吗？')) {
                var f = new FormData();
                f.append('agentId', agent.id);
                api_robot_delete(f, function (res) {
                    if (res.code == 200) {
                        this.$message.success('操作成功');
                        window.location.reload();
                    } else {
                        this.$message.error('操作失败: ' + res.message);
                    }
                }.bind(this))
            }
        },
        handleCopyOne: function (agent) {
            if (confirm('确定要复制这个机器人吗？')) {
                var f = new FormData();
                f.append('agentId', agent.id);
                api_robot_copy(f, function (res) {
                    if (res.code == 200) {
                        this.$message.success('操作成功');
                        setTimeout(function () {
                            window.location.reload();
                        }, 1000)
                    } else {
                        this.$message.error('操作失败: ' + res.message);
                    }
                }.bind(this))
            }
        },
        goDetailPage: function (agent) {
            window.location.href = '/robot/detail/' + agent.agentCode
        }
    }
})